package tw.twandroidproj.app.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.common.base.MoreObjects;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.model.Job;
import tw.twandroidproj.app.network.AuthManager;
import tw.twandroidproj.app.network.NetworkManager;
import tw.twandroidproj.app.network.OnResponseListener;

/**
 * Created by Lin on 2016/4/16.
 * Redo by Raymon on 2016/11/5
 */

public class job_create extends ActionBarActivity {
    private EditText newjob_name;
    private TextView newjob_type;
    private EditText newjob_count;
    private EditText newjob_salary;
    private TextView newjob_salary_type;
    private TextView newjob_salarytime;
    private TextView newjob_salaryway;
    private TextView newjob_timestart;
    private TextView newjob_timeend;
    private CalendarDay time_start;
    private CalendarDay time_end;
     private int payway;
    private static final DateFormat FORMATTER = SimpleDateFormat.getDateInstance();
    MaterialCalendarView calendar;
    private TextView newjob_position;
    private EditText newjob_phone;
    private EditText newjob_person;
    private Button newjob_button;
    private NetworkManager create_job_net;
    private AuthManager create_job_auth;
    private String detail = null;
   private  int salary_type;
    NetworkManager networkManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.job_create);
        networkManager=NetworkManager.getInstance(this);


        create_job_net=create_job_net.getInstance(this);
        create_job_auth=create_job_auth.getInstance(this);
        newjob_name= (EditText) findViewById(R.id.newJob_name);
        newjob_name.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if(newjob_name.getText().toString().trim().isEmpty())
                    newjob_name.setError("此项必填");

            }
        });
        newjob_type = (TextView) findViewById(R.id.newJob_type);
        final RelativeLayout layout_jobtype = (RelativeLayout) findViewById(R.id.jobtype);
        final String[] types = {"传单派送", "餐饮类", "销售类", "客服", "家教老师", "快递", "外卖送餐", "校园代理", "礼仪模特", "充场观众", "其它兼职"};
        layout_jobtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder_jobtype = new AlertDialog.Builder(job_create.this);
                builder_jobtype.setItems(types, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newjob_type.setText(types[which]);
                        newjob_type.setTextColor(getResources().getColor(R.color.bg_Black));
                    }
                });
                builder_jobtype.show();
            }
        });
        newjob_count = (EditText) findViewById(R.id.newJob_count);
        newjob_count.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (newjob_count.getText().toString().trim().isEmpty())
                    newjob_count.setError("人数必填");
            }
        });
        newjob_salary = (EditText) findViewById(R.id.newJob_salary);
        newjob_salary.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (newjob_salary.getText().toString().trim().isEmpty())
                    newjob_salary.setError("人数必填");
            }
        });

        newjob_salary_type = (TextView) findViewById(R.id.newJob_salarytype);
        final RelativeLayout layout_salarytype = (RelativeLayout) findViewById(R.id.jobsalarytype);
        final String[] salarytype = {"元/天", "元/次", "元/时", "元/月"};
        layout_salarytype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder_salarytype = new AlertDialog.Builder(job_create.this);
                builder_salarytype.setItems(salarytype, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newjob_salary_type.setText(salarytype[which]);
                        newjob_salary_type.setTextColor(getResources().getColor(R.color.bg_Black));
                    }
                });
                builder_salarytype.show();
            }
        });

        newjob_salarytime = (TextView) findViewById(R.id.newJob_salarytime);
        final RelativeLayout layout_salarytime = (RelativeLayout) findViewById(R.id.jobsalarytime);
        final String[] salarytime = {"完工结算", "当场结算", "月末结算", "周末结算"};
        layout_salarytime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder_salarytime = new AlertDialog.Builder(job_create.this);
                builder_salarytime.setItems(salarytime, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        salary_type=which;
                        newjob_salarytime.setText(salarytime[which]);


                    }
                });
                builder_salarytime.show();
            }
        });

        newjob_salaryway = (TextView) findViewById(R.id.newJob_salaryway);
        final RelativeLayout layout_salaryway = (RelativeLayout) findViewById(R.id.jobsalaryway);
        final String[] salaryway = {"在线支付", "现金支付", "其它"};
        layout_salaryway.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder_salaryway = new AlertDialog.Builder(job_create.this);
                builder_salaryway.setItems(salaryway, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        newjob_salaryway.setText(salaryway[which]);
                        payway=which;
                    }
                });
                builder_salaryway.show();
            }
        });

        newjob_timestart = (TextView) findViewById(R.id.newJob_starttime);
        final RelativeLayout layout_start = (RelativeLayout) findViewById(R.id.jobstart);
        layout_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(job_create.this);
                LayoutInflater inflater = LayoutInflater.from(job_create.this);
                final View dia_view = inflater.inflate(R.layout.calendar_dialog, null);
                calendar = (MaterialCalendarView) dia_view.findViewById(R.id.calendarView);
                builder.setView(dia_view);
                final AlertDialog dialog = builder.create();
                dialog.show();
                final Button time_Certain = (Button) dia_view.findViewById(R.id.time_Certain);
                final Button time_cancel = (Button) dia_view.findViewById(R.id.time_cancel);

                time_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                calendar.setOnDateChangedListener(new OnDateSelectedListener() {
                    @Override
                    public void onDateSelected(MaterialCalendarView widget, final CalendarDay date, boolean selected) {
                        time_Certain.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                time_start = calendar.getSelectedDate();
                                if (time_start.isBefore(CalendarDay.today())) {
                                    Toast.makeText(job_create.this, "不能比当前时间早！！！", Toast.LENGTH_SHORT).show();
                                } else {
                                    newjob_timestart.setText(FORMATTER.format(date.getDate()));
                                    newjob_timestart.setTextColor(getResources().getColor(R.color.bg_Black));
                                }
                                dialog.dismiss();
                            }
                        });
                    }
                });
            }
        });


        newjob_timeend = (TextView) findViewById(R.id.newJob_endtime);
        final RelativeLayout layout_end = (RelativeLayout) findViewById(R.id.jobend);
        layout_end.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(job_create.this);
                LayoutInflater inflater = LayoutInflater.from(job_create.this);
                final View dia_view = inflater.inflate(R.layout.calendar_dialog, null);
                calendar = (MaterialCalendarView) dia_view.findViewById(R.id.calendarView);
                builder.setView(dia_view);
                final AlertDialog dialog = builder.create();
                dialog.show();
                final Button time_Certain = (Button) dia_view.findViewById(R.id.time_Certain);
                final Button time_cancel = (Button) dia_view.findViewById(R.id.time_cancel);

                time_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                calendar.setOnDateChangedListener(new OnDateSelectedListener() {
                    @Override
                    public void onDateSelected(MaterialCalendarView widget, final CalendarDay date, boolean selected) {
                        time_Certain.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                time_end = calendar.getSelectedDate();
                                if (time_start == null) {
                                    Toast.makeText(job_create.this, "请先选择开始时间", Toast.LENGTH_SHORT).show();
                                } else if (time_end.isBefore(CalendarDay.today())) {
                                    Toast.makeText(job_create.this, "不能比当前时间早！！！", Toast.LENGTH_SHORT).show();
                                } else if (time_end.isBefore(time_start)) {
                                    Toast.makeText(job_create.this, "不能比开始时间早！！！", Toast.LENGTH_SHORT).show();
                                } else {
                                    newjob_timeend.setText(FORMATTER.format(date.getDate()));
                                    newjob_timeend.setTextColor(getResources().getColor(R.color.bg_Black));
                                }
                                dialog.dismiss();
                            }
                        });
                    }
                });
            }
        });





        newjob_position = (TextView) findViewById(R.id.newJob_position);
        final RelativeLayout layout_position = (RelativeLayout) findViewById(R.id.jobposition);        //地图选点写这里
        layout_position.setOnClickListener(new View.OnClickListener() {                            //在地图上选择位置，这里返回地址
            @Override
            public void onClick(View v) {


                newjob_type.setTextColor(getResources().getColor(R.color.bg_Black));
            }
        });


        final RelativeLayout layout_more = (RelativeLayout) findViewById(R.id.jobmore);
        layout_more.setOnClickListener(new View.OnClickListener() {                                //跳到新的activity设置限制信息还有雇主写的提醒
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setClass(job_create.this, job_creat_more.class);
                startActivityForResult(intent, 0);


                newjob_type.setTextColor(getResources().getColor(R.color.bg_Black));
            }
        });

        newjob_person = (EditText) findViewById(R.id.newJob_person);
        if (create_job_auth.getLoginUser() != null) {                                      //自动填入昵称，用户如果不是实名可以自己改
            newjob_person.setText(create_job_auth.getLoginUser().getNickname());
        }
        newjob_phone = (EditText) findViewById(R.id.newJob_phone);                           //自动填入联系方式
        if (create_job_auth.getLoginUser() != null) {
            newjob_phone.setText(create_job_auth.getLoginUser().getContact());
        }

        newjob_button = (Button) findViewById(R.id.newJob_button);


        newjob_button.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Toast.makeText(job_create.this, detail, Toast.LENGTH_SHORT).show();
                networkManager.releaseJob(newjob_name.getText().toString(), newjob_type.getText().toString(),newjob_salary.getText().toString() ,
                                            newjob_count.getText().toString(),detail,String.valueOf(payway),
                                            newjob_timestart.getText().toString(),newjob_timeend.getText().toString(),
                        new OnResponseListener<Job>()
                {
                    @Override
                    public void onSuccess(Job result) {
                         startActivity(new Intent(job_create.this,MainActivity.class));
                    }

                    @Override
                    public void onFailed(ErrorModel errorModel) {
                            Toast.makeText(getBaseContext(), newjob_type.getText(), Toast.LENGTH_LONG).show();

                    }
                });

            }
        });
    /*
    没有公司和公司id
    */


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 1) {
            Bundle bundle_detail = data.getExtras();
            detail = bundle_detail.getString("detail");
            Toast.makeText(job_create.this, detail, Toast.LENGTH_SHORT).show();
        }

        super.onActivityResult(requestCode, resultCode, data);

    }
}